package bookstore.servlet.client;

import bookstore.entity.User;
import bookstore.service.UserService;
import bookstore.service.impl.UserServiceImpl;
import bookstore.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

@WebServlet("/client/user.do")
public class UserServlet extends BaseServlet {
             UserService userService=new UserServiceImpl();

    //1.登录 ?action=login
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        User login = userService.login(user);

        if (login != null){  //表示登陆成功，登陆成功进行跳转到网站的首页
            //使用请求重定向发到达网站首页response.sendRedirect()请求重定向

                //request.getContextPath() 获取的工程路径
                response.sendRedirect(request.getContextPath()+"/index.html");

        }else {   //登陆失败重新回到登陆页面
            response.sendRedirect("./login.html");
        }

    }
    //2.注册  ?action=register
    public void register(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        //设置返回的编码，防止乱码
        request.setCharacterEncoding("utf-8");  //请求编码
        response.setCharacterEncoding("utf-8"); //响应编码
        //设置页面编码格式防止乱码
        response.setContentType("text/html;charset=utf-8");  //服务器返回给前端的编码，防止返回是中文的错误

        String email = request.getParameter("email");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        String man = request.getParameter("man");
        String girl = request.getParameter("girl");



        String sex=null;
        if (man!=null){
            sex="男";
        }

        if (girl!=null){
            sex="女";
        }



        String telephone = request.getParameter("telephone");

        String introduce = request.getParameter("introduce");


        User user = new User(null,username,password,sex,email,telephone,introduce,"1",1,1,new Date());





        //获取输出流对象
        try {
            //获取输出流对象
            PrintWriter writer = response.getWriter();



            //调用查询用户名是否已经存在
            boolean userae=userService.isExistsUsername(username);

            if (userae){
                writer.write("<h1>注册失败，会员名称已经存在<h1>");
              }


            if (!userae){
                //调用注册方法
                int land =userService.register(user);
                if (land>0){  //返回了记录，表示注册成功
                    writer.write("<h1>用户注册成功！<h1>");
                }else {
                    writer.write("<h1>用户注册失败！<h1>");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
